package com.zaofan.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.zaofan.bean.OpenArea;
import com.zaofan.bean.OpenBuilder;
import com.zaofan.bean.OpenCity;
import com.zaofan.constant.StateConstant;
import com.zaofan.dao.base.DaoSupportImpl;

public class OpenBuilderDao extends DaoSupportImpl<OpenBuilder> {

	public void updateCityName(OpenCity city) {
		try {
			String sql = "update " + OpenBuilder.class.getSimpleName().toLowerCase() + " set cityName = '"
					+ city.getCityName() + "' where cityId = " + city.getId();
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void updateAreaName(OpenArea area) {
		try {
			String sql = "update " + OpenBuilder.class.getSimpleName().toLowerCase() + " set areaName = '"
					+ area.getAreaName() + "' where areaId = " + area.getId();
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void updateState(OpenBuilder openBuilder) {
		try {
			String sql = "update " + OpenBuilder.class.getSimpleName().toLowerCase() + " set state = "
					+ openBuilder.getState() + " where id = " + openBuilder.getId();
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public List<OpenBuilder> findByNameLike(String key, String cityId) {
		try {
			String sql = "select * from " + OpenBuilder.class.getSimpleName().toLowerCase() + " where state = "
					+ StateConstant.STATE_SHOW + " and builderName like '%" + key + "%' ";
			if (cityId != null && !cityId.trim().equals("")) {
				sql = sql + " and cityId=" + cityId;
			}
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new BeanListHandler<>(OpenBuilder.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void updateStateUnShowByAreaId(int areaId) {
		try {
			String sql = "update " + OpenBuilder.class.getSimpleName().toLowerCase() + " set state = "
					+ StateConstant.STATE_UN_SHOW + " where areaId = " + areaId;
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void updateStateUnShowByCityId(int cityId) {
		try {
			String sql = "update " + OpenBuilder.class.getSimpleName().toLowerCase() + " set state = "
					+ StateConstant.STATE_UN_SHOW + " where cityId = " + cityId;
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public OpenBuilder findByAddressId(int addressId){
		try {
			String sql = "select * from " + OpenBuilder.class.getSimpleName().toLowerCase() + " where state = "
					+ StateConstant.STATE_SHOW + " and id = " +addressId;

			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new BeanHandler<>(OpenBuilder.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
